package com.china08.yxyapi.service.updateresource.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.res.YxyResResourceForEsReindex;
import com.china08.yxyapi.entity.db.res.YxyResWeikeKnow;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.db.YxyResResourceForEsReindexRepository;
import com.china08.yxyapi.repository.db.YxyResWeikeKnowRepository;
import com.china08.yxyapi.service.updateresource.WeikeUpdateService;

@Service
public class WeikeUpdServiceImpl implements WeikeUpdateService {

    @Autowired
    private YxyQuesKnowRepository yxyQuesKnowRepository;

    @Autowired
    private YxyResWeikeKnowRepository yxyResWeikeKnowRepository;

    @Autowired
    private YxyResResourceForEsReindexRepository yxyResResourceForEsReindexRepository;

    @Override
    public void updateWeikeBase(List<YxyQuesKnow> knowledges) {
        yxyQuesKnowRepository.save(knowledges);
    }

    @Override
    @Transactional
    public void updateWeikeByKnowledges(List<YxyQuesKnow> knowledges) {
        for (YxyQuesKnow knowledge : knowledges) {
            YxyQuesKnow yxyKnowledge = yxyQuesKnowRepository.findById(knowledge.getId());
            if (yxyKnowledge == null) {
                continue;
            }
            yxyKnowledge.setIsUpdate(0);
            yxyQuesKnowRepository.save(yxyKnowledge);
            //
            Integer knowId = knowledge.getId();

            // 根据当前知识点更改微课知识点关联
            List<YxyResWeikeKnow> weikeKnows = yxyResWeikeKnowRepository.findByKnowId(knowId);
            updKnow(knowId, weikeKnows);

            // 根据 1级知识点字段查询的数据，循环使用直接关联的知识点（knowId） 重新赋值 1 2 级字段
            String strKnowId = knowId + "";
            List<YxyResWeikeKnow> oneWeikeKnows = yxyResWeikeKnowRepository.findByKnowOneLevelId(strKnowId);
            for (YxyResWeikeKnow yxyResWeikeKnow : oneWeikeKnows) {
                List<YxyResWeikeKnow> know1List = new ArrayList<YxyResWeikeKnow>();
                know1List.add(yxyResWeikeKnow);
                updKnow(yxyResWeikeKnow.getKnowId(), know1List);
            }
            // 根据 2级知识点字段查询的数据，循环使用直接关联的知识点（knowId） 重新赋值 1 2 级字段
            List<YxyResWeikeKnow> twoWeikeKnows = yxyResWeikeKnowRepository.findByKnowTwoLevelId(strKnowId);
            for (YxyResWeikeKnow yxyResWeikeKnow : twoWeikeKnows) {
                List<YxyResWeikeKnow> know2List = new ArrayList<YxyResWeikeKnow>();
                know2List.add(yxyResWeikeKnow);
                updKnow(yxyResWeikeKnow.getKnowId(), know2List);
            }
        }
    }

    public void updKnow(Integer knowId, List<YxyResWeikeKnow> weikeKnows) {

        YxyQuesKnow knowledge = yxyQuesKnowRepository.findById(knowId);
        if (knowledge == null) { return; }

        String knowName = knowledge.getName();

        // 二级
        Integer knoLevel2Id = knowledge.getParentId();
        String knoLevel2Name = null;
        // 一级
        Integer knoLevel1Id = null;
        String knoLevel1Name = null;
        if (knoLevel2Id != null) {
            YxyQuesKnow knowTwo = yxyQuesKnowRepository.findById(knoLevel2Id);
            if (knowTwo != null) {
                knoLevel2Name = knowTwo.getName();
                knoLevel1Id = knowTwo.getParentId();
            }
        }
        if (knoLevel1Id != null) {
            YxyQuesKnow knowOne = yxyQuesKnowRepository.findById(knoLevel1Id);
            if (knowOne != null) {
                knoLevel1Name = knowOne.getName();
            }
        }
        for (YxyResWeikeKnow yxyResWeikeKnow : weikeKnows) {
            yxyResWeikeKnow.setKnowName(knowName);
            //
            yxyResWeikeKnow.setKnowOneLevelId(knoLevel1Id + "");
            yxyResWeikeKnow.setKnowOneLevelName(knoLevel1Name);
            yxyResWeikeKnow.setKnowTwoLevelId(knoLevel2Id + "");
            yxyResWeikeKnow.setKnowTwoLevelName(knoLevel2Name);

            yxyResWeikeKnowRepository.save(yxyResWeikeKnow);
            YxyResResourceForEsReindex esReindex = new YxyResResourceForEsReindex();
            esReindex.setResourceId(yxyResWeikeKnow.getWeikeId());
            yxyResResourceForEsReindexRepository.save(esReindex);
        }
    }

}
